package com.yanwq.sayhi.controller.article.activity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yanwq.ijkplayer.widget.media.AndroidMediaController;
import com.yanwq.ijkplayer.widget.media.IjkVideoView;
import com.yanwq.sayhi.R;
import com.yanwq.sayhi.base.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;
import tv.danmaku.ijk.media.player.IMediaPlayer;

public class VideoPlayActivity extends BaseActivity {
    @BindView(R.id.tv_video_path)
    TextView mTvVideoPath;
    @BindView(R.id.vv_playback)
    IjkVideoView mVvPlayback;
    @BindView(R.id.vp_play)
    ImageView mIVPlay;

    @OnClick(R.id.vp_play)
    void play() {
        if (mIVPlay.getTag() == null) {
            mIVPlay.setImageResource(R.drawable.ic_stop_white_24dp);
            mIVPlay.setTag("playing");
            mVvPlayback.start();
        } else if (mIVPlay.getTag().toString().equals("playing")) {
            mIVPlay.setImageResource(R.drawable.ic_play_arrow_white_24dp);
            mIVPlay.setTag("pause");
            mVvPlayback.pause();
        } else if (mIVPlay.getTag().toString().equals("pause")) {
            mIVPlay.setImageResource(R.drawable.ic_stop_white_24dp);
            mIVPlay.setTag("playing");
            mVvPlayback.start();
        }
    }

    String mPath;
    int mVideoCurPos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);
        hideStatusBar(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVvPlayback.stopPlayback();
    }

    @Override
    protected void onPause() {
        super.onPause();
        /*mVvPlayback.pause();
        mVideoCurPos = mVvPlayback.getCurrentPosition();*/
    }

    @Override
    protected void onResume() {
        super.onResume();
        /*mVvPlayback.seekTo(mVideoCurPos);
        mVvPlayback.start();*/
    }

    @Override
    public void init() {
        mPath = getIntent().getStringExtra("path");
    }

    @Override
    public void initView() {
        mTvVideoPath.setText(mPath);
        mVvPlayback.setVideoPath(mPath);
        mVvPlayback.setKeepScreenOn(true);
        //mVvPlayback.setMediaController(new AndroidMediaController(this));
        mVvPlayback.setOnInfoListener(new IMediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(IMediaPlayer mp, int what, int extra) {
                Toast.makeText(getApplicationContext(), "what:" + what, Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        mVvPlayback.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(IMediaPlayer mp) {

            }
        });

        mVvPlayback.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(IMediaPlayer mp) {
                onBackPressed();
            }
        });

        //mVvPlayback.start();
    }
}
